package com.intel.factorytest.runin;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.content.Context;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.SystemProperties;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.util.Log;
import android.widget.Toast;

import com.intel.factorytest.R;

public class RunInActivity extends PreferenceActivity {
    
     private static final String TAG = "RunInActivity";
     private boolean isExit = false;
     private Preference mBtPreference;
     private PowerManager.WakeLock wl; 
     static final String PARTIAL_NAME = "BatteryMonitorActivity";
    /** Called when the activity is first created. */
     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.runin_test);    
        mBtPreference = (Preference) findPreference("battery_pref");
        callShellCommand();
        if (isExit) {
        mBtPreference.setEnabled(false);
        }
    } 
    public void callShellCommand(){

        try {
            Process p = Runtime.getRuntime().exec(new String[]{"sh","-c","ps | grep -r BatteryMonitor"});
            InputStream is = p.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String tmp = "";
            while((tmp=br.readLine())!=null){
                Log.i("yjc",tmp);
                if(tmp.contains("factorytest")) {
                    isExit = true;
                }
            }
            br.close();

            InputStream eis = p.getErrorStream();
            InputStreamReader eisr = new InputStreamReader(eis);
            BufferedReader ebr = new BufferedReader(eisr);

            while((tmp=ebr.readLine())!=null){
                Log.e("yjc",tmp);
            }
            ebr.close();
                
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    public boolean onPreferenceTreeClick(PreferenceScreen prefScreen, Preference pref)
    {
        switch(pref.getOrder())
        {
             case 0://ServiceInfoMenu
                 {
                     SystemProperties.set("ctl.start","BatteryMonitor");
                     PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
                     wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, PARTIAL_NAME);
                     wl.setReferenceCounted(false);
                     wl.acquire();
                     Toast.makeText(RunInActivity.this, "you start the BatteryMonitor test ", Toast.LENGTH_SHORT).show();
                     finish();
                     break;
                 }
             default:
                 break;
         }
         return false;        
     }
}
